package com.ruoyi.find.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.find.mapper.FurnitureTypeMapper;
import com.ruoyi.find.domain.FurnitureType;
import com.ruoyi.find.service.IFurnitureTypeService;

/**
 * 家具类型Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-24
 */
@Service
public class FurnitureTypeServiceImpl implements IFurnitureTypeService 
{
    @Autowired
    private FurnitureTypeMapper furnitureTypeMapper;

    /**
     * 查询家具类型
     * 
     * @param typeId 家具类型主键
     * @return 家具类型
     */
    @Override
    public FurnitureType selectFurnitureTypeByTypeId(Long typeId)
    {
        return furnitureTypeMapper.selectFurnitureTypeByTypeId(typeId);
    }

    /**
     * 查询家具类型列表
     * 
     * @param furnitureType 家具类型
     * @return 家具类型集合
     */
    @Override
    public List<FurnitureType> selectFurnitureTypeList(FurnitureType furnitureType)
    {
        return furnitureTypeMapper.selectFurnitureTypeList(furnitureType);
    }

    /**
     * 新增家具类型
     * 
     * @param furnitureType 家具类型
     * @return 结果
     */
    @Override
    public int insertFurnitureType(FurnitureType furnitureType)
    {
        // 设置层级
        if (furnitureType.getParentId() == null || furnitureType.getParentId() == 0) {
            furnitureType.setLevel(1);
        } else {
            FurnitureType parentType = furnitureTypeMapper.selectFurnitureTypeByTypeId(furnitureType.getParentId());
            furnitureType.setLevel(parentType.getLevel() + 1);
        }
        furnitureType.setCreateTime(DateUtils.getNowDate());
        return furnitureTypeMapper.insertFurnitureType(furnitureType);
    }

    /**
     * 修改家具类型
     * 
     * @param furnitureType 家具类型
     * @return 结果
     */
    @Override
    public int updateFurnitureType(FurnitureType furnitureType)
    {
        furnitureType.setUpdateTime(DateUtils.getNowDate());
        return furnitureTypeMapper.updateFurnitureType(furnitureType);
    }

    /**
     * 批量删除家具类型
     * 
     * @param typeIds 需要删除的家具类型主键集合
     * @return 结果
     */
    @Override
    public int deleteFurnitureTypeByTypeIds(Long[] typeIds)
    {
        return furnitureTypeMapper.deleteFurnitureTypeByTypeIds(typeIds);
    }

    /**
     * 删除家具类型信息
     * 
     * @param typeId 家具类型主键
     * @return 结果
     */
    @Override
    public int deleteFurnitureTypeByTypeId(Long typeId)
    {
        // 检查是否有子类型
        List<FurnitureType> childTypes = furnitureTypeMapper.selectChildTypesByParentId(typeId);
        if (childTypes != null && !childTypes.isEmpty()) {
            throw new RuntimeException("该类型下有子类型，无法删除");
        }
        return furnitureTypeMapper.deleteFurnitureTypeByTypeId(typeId);
    }

    /**
     * 查询所有启用的家具类型
     * 
     * @return 家具类型列表
     */
    @Override
    public List<FurnitureType> selectAllEnabledFurnitureTypes()
    {
        return furnitureTypeMapper.selectAllEnabledFurnitureTypes();
    }

    /**
     * 根据父类型ID查询子类型
     * 
     * @param parentId 父类型ID
     * @return 子类型列表
     */
    @Override
    public List<FurnitureType> selectChildTypesByParentId(Long parentId)
    {
        return furnitureTypeMapper.selectChildTypesByParentId(parentId);
    }

    /**
     * 启用家具类型
     * 
     * @param typeId 类型ID
     * @return 结果
     */
    @Override
    public int enableFurnitureType(Long typeId)
    {
        FurnitureType furnitureType = new FurnitureType();
        furnitureType.setTypeId(typeId);
        furnitureType.setStatus(1);
        furnitureType.setUpdateTime(DateUtils.getNowDate());
        return furnitureTypeMapper.updateFurnitureType(furnitureType);
    }

    /**
     * 禁用家具类型
     * 
     * @param typeId 类型ID
     * @return 结果
     */
    @Override
    public int disableFurnitureType(Long typeId)
    {
        FurnitureType furnitureType = new FurnitureType();
        furnitureType.setTypeId(typeId);
        furnitureType.setStatus(0);
        furnitureType.setUpdateTime(DateUtils.getNowDate());
        return furnitureTypeMapper.updateFurnitureType(furnitureType);
    }
}